<template>
  <!-- 企业审核对话框 -->
  <el-dialog v-dialogDrag class="dialogStyleMiddle" append-to-body :lock-scroll="false" :title="dialogTitle" :visible="dialogVisible" :before-close="cancel">
    <el-form ref="addSysEnterpriseForm" label-position="left" :model="sysEnterprise" label-width="100px" label-suffix="：">
      <!-- <el-row>
        <el-col :span="24"><div class="grid-content bg-purple">企业信息</div></el-col>
      </el-row> -->
      <el-col :span="12">
        <el-form-item label="企业名称" prop="name">{{ sysEnterprise.name }}</el-form-item>
      </el-col>
      <!-- <el-col :span="12">
        <el-form-item label="企业类型" prop="enterpriseType">
          <span v-show="sysEnterprise.enterpriseType === '0'">一般企业</span>
          <span v-show="sysEnterprise.enterpriseType === '1'">监管单位</span>
        </el-form-item>
      </el-col> -->
      <el-col :span="12">
        <el-form-item label="企业性质" prop="nature">{{ sysEnterprise.nature }}</el-form-item>
      </el-col>
      <!-- <el-col :span="12">
        <el-form-item label="信用代码" prop="creditCodePic">{{ sysEnterprise.creditCodePic }}</el-form-item>
      </el-col> -->
      <el-col :span="12">
        <el-form-item label="企业地址">
          {{ currentProvince }}
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="详细地址" prop="address">{{ sysEnterprise.address }}</el-form-item>
      </el-col>
      <!-- <el-col :span="12">
        <el-form-item label="企业法人" prop="legalperson">{{ sysEnterprise.legalperson }}</el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="法人电话" prop="legalpersonMobile">{{ sysEnterprise.legalpersonMobile }}</el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item prop="contacts">
          <label slot="label">联&nbsp;&nbsp;系&nbsp;&nbsp;人：</label>
          {{ sysEnterprise.contacts }}
        </el-form-item>
      </el-col> -->
      <!-- <el-col :span="12">
        <el-form-item label="身份证号" prop="idNumber">{{ sysEnterprise.idNumber }}</el-form-item>
      </el-col> -->
      <el-col :span="12">
        <el-form-item label="联系人" prop="contacts">
          <label slot="label" v-html="`联&ensp;系&ensp;人：`" />
          {{ sysEnterprise.contacts }}
          <!-- <el-input v-model="sysEnterprise.contacts" placeholder="请输入联系人姓名" size="medium" /> -->
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="联系电话" prop="phone">{{ sysEnterprise.phone }}</el-form-item>
      </el-col>
      <!-- <el-col :span="12">
        <el-form-item prop="seller" label="销售代表">
          {{ sysEnterprise.seller }}
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item prop="counterpart" label="客服代表">
          {{ sysEnterprise.counterpart }}
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="资质证书" prop="qualificationUrl">{{ sysEnterprise.qualificationUrl }}</el-form-item>
      </el-col> -->
      <!-- <el-col :span="10" style="margin-top:-15px;">
        <el-form-item prop="unitCreditCodePicUrl">
          <span slot="label">企业信用&nbsp;&nbsp;&nbsp;&nbsp;<br>代码扫描件</span>
          <FileViewNoTable ref="fileView" style="margin-top:-10px;" :edit-file-list="editFileList" />
        </el-form-item>
      </el-col> -->
      <!-- <el-row>
        <el-col :span="24"><div class="grid-content bg-purple">审核操作</div></el-col>
      </el-row> -->
      <el-col :span="12">
        <el-form-item label="企业审核" prop="checkStatus">
          <el-radio-group v-model="sysEnterprise.checkStatus">
            <el-radio label="0">审核成功</el-radio>
            <el-radio label="1">审核失败</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="medium" @click="cancel">取 消</el-button>
      <el-button size="medium" :disabled="btnDisabled" type="primary" @click="submitForm('addSysEnterpriseForm')">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getBasisById, updateBasis } from '@/api/enterprise/basis'
// 附件回显
// import FileViewNoTable from '@/components/FileUplode/FileView'
import { selectFileById } from '@/api/file/file.js'
import { listRegionTree } from '@/api/common/area'
export default {
  name: 'AuditEnterprise',
  components: {
    // FileViewNoTable
  },
  data() {
    return {
      dialogTitle: '',
      dialogVisible: false,
      sysEnterprise: {
        id: '',
        name: '',
        nature: '',
        creditCodePic: '',
        unitCreditCodePicUrl: '',
        qualificationUrl: '',
        counterpart: '',
        seller: '',
        province: '',
        city: '',
        area: '',
        address: '',
        legalperson: '',
        legalpersonMobile: '',
        idCard: '',
        idNumber: '',
        contacts: '',
        phone: '',
        enterpriseType: '0',
        checkStatus: '0',
        status: ''
      },
      currentProvince: '', // 地址
      editFileList: [],
      btnDisabled: false
    }
  },
  created() {
    this.getDicts('enterprise_nature').then(response => {
      this.natureData = response.data.rows
    })
    this.getCityOptions()
  },
  methods: {
    // 设置提交的值为空
    emptySysEnterprise() {
      this.resetForm('addSysEnterpriseForm')
      this.editFileList = []
    },
    onSelected(data) {
      this.sysEnterprise.province = data.province.value
      this.sysEnterprise.city = data.city.value
      this.sysEnterprise.area = data.area.value
      data.province = ''
      data.city = ''
      data.area = ''
    },
    auditAdd(id) {
      this.dialogVisible = true
      this.getAudit(id)
    },
    getAudit(id) {
      // 根据id查找这条的内容
      getBasisById(id).then(resp => {
        if (resp) {
          const rowData = resp.data
          this.dialogTitle = '企业审核'
          const { id, nature, name, creditCodePic, unitCreditCodePicUrl,
            qualificationUrl, province, city, area, address, legalperson, legalpersonMobile,
            idNumber, contacts, phone, enterpriseType, counterpart, seller, status } = resp.data

          this.sysEnterprise = {
            ...this.sysEnterprise,
            id, nature, name, creditCodePic, unitCreditCodePicUrl,
            qualificationUrl, province, city, area, address, legalperson, legalpersonMobile,
            idNumber, contacts, phone, enterpriseType, counterpart, seller, status
          }
          // 翻译地址
          this.currentProvince = this.getRegnaion(province, city, area)
          for (const i in this.natureData) {
            if (this.sysEnterprise.nature === this.natureData[i].dictValue) {
              this.sysEnterprise.nature = this.natureData[i].dictLabel
            }
          }
          if (this.sysEnterprise.unitCreditCodePicUrl) {
            this.selectFildListByIds(this.sysEnterprise.unitCreditCodePicUrl)
          }
          if (rowData.checkStatus === '1') {
            this.sysEnterprise.checkStatus = '1'
          } else {
            this.sysEnterprise.checkStatus = '0'
          }
          this.dialogVisible = true
        }
      })
        .catch((err) => {
          console.log(err)
          this.msgError('编辑查找失败！')
        })
    },
    getCityOptions() {
      listRegionTree().then(res => {
        if (res.data) {
          this.cityOptions = res.data
        }
      })
    },
    getRegnaion(province, city, area) {
      const sheng = this.cityOptions.find(item => item.value === province)
      const shi = sheng.children.find(item => item.value === city)
      const xian = shi.children.find(item => item.value === area)
      return sheng.label + shi.label + xian.label
    },
    // 弹框取消按钮
    cancel() {
      this.dialogVisible = false
      this.emptySysEnterprise()
      this.btnDisabled = false
      // this.$refs.fileView && this.$refs.fileView.emptyFile()
    },
    // 根据附件id查询附件信息
    selectFildListByIds() {
      const fileIds = this.sysEnterprise.unitCreditCodePicUrl
      selectFileById(fileIds).then(resp => {
        if (resp) {
          this.editFileList = resp.data
        }
      })
    },
    // 弹框确认按钮
    submitForm(fromData) {
      this.$refs[fromData].validate(valid => {
        if (valid) {
          this.btnDisabled = true
          const { id, checkStatus, idNumber } = this.sysEnterprise
          if (id) { // 更新
            const sysEnterprise = { id, checkStatus, idNumber }
            updateBasis(sysEnterprise).then(resp => {
              if (resp) {
                this.dialogVisible = false
                this.emptySysEnterprise()
                this.$emit('childrenGetList')
                this.msgSuccess('审核完成')
                this.$refs.fileView && this.$refs.fileView.emptyFile()
              }
              this.btnDisabled = false
            }).catch(() => {
              this.btnDisabled = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .dialog-footer{
    text-align: center;
  }
</style>
